package example;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import org.pi4.locutil.GeoPosition;
import org.pi4.locutil.MACAddress;
import org.pi4.locutil.trace.TraceEntry;

/**
 * Access Point file Parser.
 * 
 */
public class APParser {

	private File file;

	public APParser(File file) {
		if (file == null)
			throw new IllegalArgumentException("file cannot be null");
		this.file = file;
	}

	public ArrayList<TraceEntry> parse() throws NumberFormatException, IOException {
				
		ArrayList<TraceEntry> list = new ArrayList<TraceEntry>();
		
		BufferedReader in = new BufferedReader(new FileReader(file));
		String line;
		
		try {
			// Process each line.
			while ((line = in.readLine()) != null) {
				if (line.startsWith("#"))
					continue;
				
				TraceEntry entry = new TraceEntry();		
				String[] components = line.split(" ");
				if(components.length == 4)
					//MAC?	
					if(components[0].split(":").length == 6){
						entry.setId(MACAddress.parse(components[0]));
					}
					entry.setGeoPosition(new GeoPosition(Double.parseDouble(components[1]), Double.parseDouble(components[2]), Double.parseDouble(components[3])));
				
				list.add(entry);
			}
		}
		finally{
			in.close();
		}
		return list;
	}
}